import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);
import {
  login,
  logout,
  getinfo
} from "@/request/api/user";
import {
  getToken,
  setToken,
  removeToken,
  getrefreshToken,
  setrefreshToken,
  removerefreshToken
} from "@/util/token";

const state = {
  refreshToken: getrefreshToken(),
  token: getToken(),
  account: "",
  avatar: "",
  userId: "",
  username: "",
  level: "",
  sex: "",
  city: "",
  birth: "",
  introduce: "",
  // 音乐的url
  musicId: "",
  // 音乐的播放状态
  isPlay: false,
  // 当前播放的歌单
  musicList: [],
  // 当前播放歌单的id
  musicListId: "",
  // 是否登录
  isLogin: false,
  // 当前播放音乐的索引
  currentIndex: -1,
  // 音乐详情卡片的显隐
  isMusicDetailCardShow: false,
  // 实时播放时长
  currentTime: 0,
  // 音乐是否在加载中
  isMusicLoad: false,
  // 在歌手详情保存的上一首歌曲信息
  currentRowInfo: {},
  // 用户喜欢的音乐列表
  likeMuiscList: [],
  // 已收藏的歌单
  // 已收藏的专辑
  subAlbumList: null,
  // 已收藏的歌手
  subSingerList: null,
  // 已收藏的video
  subVideoList: null,
  // 已收藏的歌单
  collectMusicList: [],
  // 用户创建的歌单
  createdMusicList: [],
  // 要下载的音乐信息
  downloadMusicInfo: {
    name: "",
    url: ""
  },
  // 已喜欢的视频
  likeVideoList: null
};

const store = new Vuex.Store({
  state,
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token;
    },
    SET_refreshTOKEN: (state, token) => {
      state.refreshToken = token;
    },
    SET_ACCOUNT: (state, account) => {
      state.account = account;
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_USERID: (state, id) => {
      state.userId = id;
    },
    SET_USERNAME: (state, name) => {
      state.username = name;
    },
    SET_lEVEL: (state, level) => {
      state.level = level;
    },
    SET_SEX: (state, sex) => {
      state.sex = sex;
    },
    SET_CITY: (state, city) => {
      state.city = city;
    },
    SET_BIRTH: (state, birth) => {
      state.birth = birth;
    },
    SET_INTRODUCE: (state, introduce) => {
      state.introduce = introduce;
    },
    // 更新音乐url
    updateMusicId(state, musicId) {
      state.musicId = musicId;
      // console.log('updateMusicId');
    },
    // 修改播放状态
    changePlayState(state, isPlay) {
      state.isPlay = isPlay;
      // console.log('changePlayState');
    },
    // 更改音乐播放详情卡片可见性
    changeMusicDetailCardState(state) {
      state.isMusicDetailCardShow = !state.isMusicDetailCardShow;
    },
    // 更新音乐的加载状态
    updateMusicLoadState(state, isLoad) {
      state.isMusicLoad = isLoad;
    },
    // 更新音乐当前时间
    updateCurrentTime(state, currentTime) {
      state.currentTime = currentTime;
    }
  },
  actions: {
    login({ commit }, userInfo) {
      const { account, password } = userInfo;
      return new Promise((resolve, reject) => {
        const data = {
          account: account,
          password: password
        };
        login(data)
          .then(res => {
            commit("SET_TOKEN", res.token.token);
            commit("SET_refreshTOKEN", res.token.refreshToken);
            setToken(res.token.token);
            setrefreshToken(res.token.refreshToken);
            resolve(res);
          })
          .catch(err => {
            reject(err);
          });
      });
    },

    getInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo(state.token)
          .then(res => {
            const data = res;
            if (!data) {
              reject("验证失败，请重新登录");
            }
            var { username, avatar, type } = data;
            if (!type) {
              reject("角色必须为非空数组");
            }
            commit("SET_USERNAME", username);
            commit("SET_AVATAR", avatar);
            commit("SET_TYPE", type);
            resolve(data);
          })
          .catch(err => {
            reject(err);
          });
      });
    },
    logout({ commit, state }) {
      return new Promise((resolve, reject) => {
        const data = {
          token: getToken()
        };
        logout(data).then(() => {
          commit("SET_TOKEN", "");
          commit("SET_TYPE", "");
          removeToken();
          resetRouter();
          resolve();
        });
      }).catch(err => {
        reject(err);
      });
    },
    resetToken({ commit }) {
      return new Promise(resolve => {
        commit("SET_TOKEN", "");
        commit("SET_TYPE", "");
        removeToken();
        resolve();
      });
    }
  }
});





export default store;
